home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / cit.arc / MISC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-01-22  |  17.4 KB  |  553 lines

  1. /************************************************************************/
  2. /*                misc.c                    */
  3. /*                                    */
  4. /*    Random and networking functions.                */
  5. /************************************************************************/
  6.  
  7. /************************************************************************/
  8. /*                history                 */
  9. /*                                    */
  10. /* 85Aug17 HAW    Fix putFLChar() to indicate errors.            */
  11. /* 85Jul04 HAW    Change configure() to accept parameters.        */
  12. /* 85Mar21 HAW    Loss of carrier is now detected for downloading of file.*/
  13. /* 85Mar20 HAW    Add time stamp code.                    */
  14. /* 85Mar13 HAW    Fix upLoad() to kill unsuccessful uploads.        */
  15. /* 85Feb22 HAW    Implement download/upload.                */
  16. /* 85Jan21 HAW    Fix tutorial() so invalid drive cannot be specified.    */
  17. /* 85Jan21 HAW    Insert function setdate() for MSDOS, modify changedate()*/
  18. /* 85Jan20 HAW    Insert function getdate() for MSDOS, modify changedate()*/
  19. /* 84Aug31 HAW    What's that? MS-DOS? Nooooooo!!!!            */
  20. /* 84Jul08 HAW    file_RO changed to detect R/O drives.            */
  21. /* 84Jul03 HAW    file_RO written to check .SYS files for R/O status.    */
  22. /* 84Jun23 HAW & JLS  patchDebug put under control of conditional comp. */
  23. /* 84Jun16 JLS & HAW  Configure fixed so that ".Enter Con..." works    */
  24. /* 84Jun10 JLS    Function changedate() installed.            */
  25. /* 84May01 HAW    Starting 1.50a upgrade.                 */
  26. /* 83Mar12 CrT    from msg.c                        */
  27. /* 83Mar03 CrT & SB   Various bug fixes...                */
  28. /* 83Feb27 CrT    Save private mail for sender as well as recipient.    */
  29. /* 83Feb23    Various.  transmitFile() won't drop first char on WC... */
  30. /* 82Dec06 CrT    2.00 release.                        */
  31. /* 82Nov05 CrT    Stream retrieval.  Handles messages longer than MAXTEXT.*/
  32. /* 82Nov04 CrT    Revised disk format implemented.            */
  33. /* 82Nov03 CrT    Individual history begun.  General cleanup.        */
  34. /************************************************************************/
  35.  
  36. #include "ctdl.h"
  37.  
  38. /************************************************************************/
  39. /*                contents                */
  40. /*                                    */
  41. /*    changeDate()        allow changing of date            */
  42. /*    configure()        sets terminal parameters via dialogue    */
  43. /*    crashout()        crashes out of Citadel in case of bug    */
  44. /*    doCR()            newline on modem and console        */
  45. /*    download()        menu-level routine for WC-protocol sends*/
  46. /*    getdate()        gets date from MSDOS system clock.    */
  47. /*    patchDebug()        display/patch byte            */
  48. /*    printDate()        prints out date             */
  49. /*    putFLChar()        readFile() -> disk file interface    */
  50. /*    putWCChar()        filter[]s, readFile() to putMsgChar()    */
  51. /*    setdate()        sets the date                */
  52. /*    transmitFile()        send a host file, no formatting     */
  53. /*    tutorial()        prints a .hlp file            */
  54. /*    upLoad()        menu-level read-via-WC-protocol fn    */
  55. /*    visible()        convert control chars to letters    */
  56. /************************************************************************/
  57.  
  58. /************************************************************************/
  59. /*           External variable declarations in MISC.C        */
  60. /************************************************************************/
  61. static char    *monthTab[13] = {"", "Jan", "Feb", "Mar",
  62.                      "Apr", "May", "Jun",
  63.                      "Jul", "Aug", "Sep",
  64.                      "Oct", "Nov", "Dec" };
  65. FILE         *upfd;
  66. int         acount;
  67. int         byteRate;    /* Bytes/sec that modem is set for.    */
  68.  
  69. #define AUDIT    9000
  70. char        audit[AUDIT];
  71.  
  72. /************************************************************************/
  73. /*           External variable definitions for MISC.C        */
  74. /************************************************************************/
  75. extern struct config    cfg;        /* Lots an lots of variables    */
  76. extern struct logBuffer logBuf;     /* Person buffer        */
  77. extern struct aRoom    roomBuf;    /* Room buffer            */
  78. extern struct rTable    roomTab[];
  79. extern struct msgB    msgBuf;
  80. extern unsigned char    termNulls;    /* How many nulls?        */
  81. extern unsigned char    termWidth;    /* How many columns?        */
  82. extern char        termUpper;    /* Upper only?            */
  83. extern char        termLF;     /* Linefeeds?            */
  84. extern char        expert;     /* expert?            */
  85. extern char        aide;        /* aide?            */
  86. extern char        sendTime;    /* Show time message created?    */
  87. extern unsigned char    crtColumn;    /* where are we on screen now?    */
  88. extern char        loggedIn;    /* Is we logged in?        */
  89. extern char        outFlag;    /* Output flag            */
  90. extern char        haveCarrier;    /* Do we still got carrier?    */
  91. extern char        usingWCprotocol;/* WC flag            */
  92. extern char        prevChar;    /* previous char output     */
  93. extern char        textDownload;    /* flag             */
  94. extern char        whichIO;    /* Where I/O is         */
  95. extern char        echo;        /* Should we echo? echo? echo?    */
  96. extern FILE        *msgfl;
  97. extern FILE        *roomfl;
  98. extern FILE        *logfl;
  99. extern char        oldToo;
  100. extern int        exitValue;
  101.  
  102. /************************************************************************/
  103. /*           External function definitions for MISC.C        */
  104. /************************************************************************/
  105. char mAbort();
  106. FILE *fopen();
  107.  
  108. /************************************************************************/
  109. /*    changedate() gets the date from the aide and remembers it    */
  110. /************************************************************************/
  111. changeDate()
  112. {
  113.     int year, day, hours, minutes;
  114.     char *month;
  115.  
  116.     getdate(&year, &month, &day, &hours, &minutes);
  117.     mPrintf("Current date is: %d%s%02d\n ", year, month, day);
  118.     mPrintf("Current time is: %d:%02d\n ", hours, minutes);
  119.     if (!getYesNo("Enter a new date & time"))
  120.     return ;
  121.     setdate();
  122. }
  123.  
  124. /************************************************************************/
  125. /*    configure() sets up terminal width etc via dialogue        */
  126. /************************************************************************/
  127. configure(showVals)
  128. char showVals;
  129. {
  130.     if (showVals && loggedIn) {
  131.     mPrintf("\n Your current setup:\n ");
  132.     mPrintf("%s, ", (expert) ? "Expert" : "Non-expert");
  133.  
  134.     mPrintf("%s,\n %slinefeeds, %d nulls,",
  135.         termUpper  ?  "UPPERCASE ONLY" : "Upper and Lower case",
  136.         termLF     ?  ""           : "no ",
  137.         termNulls
  138.     );
  139.     mPrintf(" screen width is %d\n ", termWidth);
  140.     mPrintf("%s time messages created,\n ",
  141.             sendTime ? "Print" : "Do not print");
  142.     mPrintf("%s last Old message on <N>ew Message request.",
  143.             oldToo ? "Print" : "Do not print");
  144.     mPrintf("\n ");
  145.     if (!getYesNo("Reset your configuration")) return;
  146.     }
  147.  
  148.     mPrintf("\n ");
  149.     termWidth    = getNumber(" Screen width", 10, 255);
  150.     termNulls    = getNumber(" #Nulls",          0, 255);
  151.     termUpper    =!getYesNo(" Do you have lower case"     ) ? TRUE : FALSE;
  152.     termLF    = getYesNo(" Do you need Linefeeds"     ) ? TRUE : FALSE;
  153.     expert    =!getYesNo(" Do you want Helpful Hints"  ) ? TRUE : FALSE;
  154.     sendTime    = getYesNo(" Print time messages created") ? TRUE : FALSE;
  155.     oldToo = getYesNo(" Print last Old message on <N>ew Message request")
  156.                                       ? TRUE : FALSE;
  157.  
  158.     /* Now make it (only) semi-permanent     --JLS&HAW */
  159.     logBuf.lbwidth       = termWidth;
  160.     logBuf.lbnulls       = termNulls;
  161.     logBuf.lbflags.EXPERT  = expert;
  162.     logBuf.lbflags.UCMASK  = termUpper;
  163.     logBuf.lbflags.LFMASK  = termLF;
  164.     logBuf.lbflags.AIDE    = aide;
  165.     logBuf.lbflags.TIME    = sendTime;
  166.     logBuf.lbflags.OLDTOO  = oldToo;
  167. }
  168.  
  169. /************************************************************************/
  170. /*    crashout() Problems?  Out we go!!!                */
  171. /************************************************************************/
  172. crashout(message)
  173. char *message;
  174. {
  175.     FILE *fd;        /* Record some crash data */
  176.     int  i;
  177.  
  178.     exitValue = CRASH_EXIT;
  179.     outFlag = IMPERVIOUS;
  180.  mPrintf("\n Whoops!! CRASHOLA!! Thanks and bye, leave mail on Test Sys!\n ");
  181.     printf("STATUS:\nmsgfl  %d\nlogfl  %d\nroomfl %d\n",
  182.             ferror(msgfl), ferror(logfl), ferror(roomfl));
  183.     interpret(cfg.pHangUp);
  184.     fclose(msgfl);
  185.     fclose(roomfl);
  186.     fclose(logfl);
  187.     fd = fopen("crash", "w");
  188.     fprintf(fd, message);
  189.     fclose(fd);
  190.     fd = fopen("audit", "w");
  191.     for (i = 0; i < AUDIT; i++) {
  192.     fputc(audit[i], fd);
  193.     if ((i+1) % 70 == 0) fprintf(fd, "\n");
  194.     }
  195.     fprintf(fd, "\n\ncounter = %d\n", acount);
  196.     fclose(fd);
  197.     writeSysTab();
  198.     exit(exitValue);
  199. }
  200.  
  201. /************************************************************************/
  202. /*    doCR() does a newline on modem and console            */
  203. /************************************************************************/
  204. doCR()
  205. {
  206.     int i;
  207.  
  208.     crtColumn    = 1;
  209.     if (outFlag != OUTOK &&    /* output is being s(kip)ped    */
  210.     outFlag != IMPERVIOUS)
  211.     return;
  212.  
  213.     mputChar(NEWLINE);
  214.     if (haveCarrier) {
  215.     if (!usingWCprotocol) {
  216.         outMod('\r');
  217.         for (i = termNulls;  i;  i--) outMod(0);
  218.         if (termLF) outMod('\n');
  219.     } else {
  220.         sendWCChar('\r');
  221.         if (termLF) sendWCChar('\n');
  222.     }
  223.     }
  224.     prevChar    = ' ';
  225. }
  226.  
  227. /************************************************************************/
  228. /*    download() is the menu-level send-message-via-WC-protocol fn    */
  229. /************************************************************************/
  230. download(whichMess, revOrder)
  231. char whichMess, revOrder;
  232. {
  233.     outFlag    = OUTOK;
  234.     if (!expert)   tutorial("wcdown.blb");
  235.  
  236.     if (!getYesNo("Ready"))  return;
  237.  
  238.     /* here we go: */
  239.     echo        = NEITHER;
  240.     usingWCprotocol    = TRUE     ;    /* all points bulletin        */
  241.  
  242.     if (doWC(STARTUP)) {
  243.  
  244.     showMessages(whichMess, revOrder);
  245.  
  246.     doWC(FINISH);
  247.  
  248.     }
  249.  
  250.     setUp(FALSE);
  251.     usingWCprotocol    = FALSE  ;
  252. }
  253.  
  254. /************************************************************************/
  255. /*    getdate() retrieves system date and returns in the parameters    */
  256. /************************************************************************/
  257. getdate(year, month, day, hours, minutes)
  258. int *year, *day, *hours, *minutes;
  259. char **month;
  260. {
  261. #ifdef MSDOS
  262.     struct regval s;
  263.  
  264.     s.ax = 0x2a00;
  265.     sysint21(&s, &s);
  266.     *year  = s.cx - 1900;
  267.     *month = monthTab[(s.dx & 0xff00) >> 8];
  268.     *day   = (s.dx & 0x00ff);
  269.     s.ax = 0x2c00;
  270.     sysint21(&s, &s);
  271.     *hours   = (s.cx & 0xff00) >> 8;
  272.     *minutes = s.cx & 0x00ff;
  273. #else
  274.     *year  = interpret(pGetYear);
  275.     *month = interpret(pGetMonth);
  276.     *day   = interpret(pGetDay);
  277.     *hours = *minutes = 0;
  278. #endif
  279. }
  280.  
  281. #ifdef XYZZY
  282. /************************************************************************/
  283. /*    patchDebug()                            */
  284. /************************************************************************/
  285. patchDebug() {
  286.     unsigned  i, j, page;
  287.     char      finished, *pc;
  288.  
  289.     mPrintf("\bpatch\n ");
  290.     page = getNumber("page", 0, 255);
  291.     finished = FALSE;
  292.     do {
  293.     for (i=16, pc= (page*256);  i;    i--)   {
  294.         mPrintf("%d ", pc);
  295.         for (j=16;    j;  j--)   {
  296.         mPrintf("%c", visible(*pc++));
  297.         }
  298.         mPrintf("\n ");
  299.     }
  300.     switch (toLower(iChar()))   {
  301.     case 'r':
  302.         pc    = getNumber("adr", 0, 65355);
  303.         *pc = getNumber("val", 0, 255);
  304.         break;
  305.     case 'n':    page++;         break;
  306.     case 'p':    page--;         break;
  307.     default:    finished = TRUE;    break;
  308.     }
  309.     } while (!finished);
  310. }
  311. #endif
  312. /************************************************************************/
  313. /*    printDate() prints out current date.                */
  314. /************************************************************************/
  315. printDate()
  316. {
  317.     int  day, year, h, m;
  318.     char *month;
  319.  
  320.     getdate(&year, &month, &day, &h, &m);
  321.     mPrintf("%d%s%02d ", year, month, day);
  322. }
  323.  
  324. /************************************************************************/
  325. /*    putFLChar() is used to upload files                */
  326. /*    returns: ERROR on problems else TRUE                */
  327. /************************************************************************/
  328. putFLChar(c)
  329. char c;
  330. {
  331.     if (fputc(c, upfd) != EOF) return TRUE;
  332.     /* else */               printf("Write error: %d\n", ferror(upfd));
  333.                    return ERROR;
  334. }
  335.  
  336. /************************************************************************/
  337. /*    putWCChar() filter[]s from readFile() to putMsgChar()        */
  338. /*    Returns: ERROR if problems, else TRUE                */
  339. /*    Globals: outFlag is set to OUTSKIP when an ascii EOF (^Z) is    */
  340. /*        encountered, and no further text is written to disk.    */
  341. /************************************************************************/
  342. int putWCChar(c)
  343. int c;
  344. {
  345.     if (outFlag)    return TRUE;    /* past ascii EOF.  ignore.    */
  346.  
  347.     if (c == ERROR) {
  348.     /* EOF.  Note for overkill:    */
  349.     outFlag = OUTSKIP;
  350.     return TRUE;
  351.     }
  352.  
  353.     c    &= 0x7F;    /* cut high stuff off    */
  354.  
  355.     if (c == CPMEOF) {
  356.     /* ascii EOF.  Ignore rest of file:    */
  357.     outFlag = OUTSKIP;
  358.     return TRUE;
  359.     }
  360.  
  361.     return  putMsgChar(cfg.filter[c]);
  362. }
  363.  
  364. /************************************************************************/
  365. /*    setdate() Sets the date.                    */
  366. /************************************************************************/
  367. setdate()
  368. {
  369. #ifdef MSDOS
  370.     struct regval s;
  371.  
  372.     while (1) {
  373.     s.cx  = getNumber("Year",  85, 99) + 1900;
  374.     s.dx  = getNumber("Month", 1,  12) << 8  ;
  375.     s.dx += getNumber("Day",   1,  31)     ;
  376.     s.ax = 0x2b00;
  377.     sysint21(&s, &s);
  378.     if (s.ax & 0xff)
  379.         mPrintf("Invalid date\n ");
  380.     else
  381.         break;
  382.     }
  383.     while (1) {
  384.     s.cx  = getNumber("Hour",   0, 23) << 8;
  385.     s.cx += getNumber("Minute", 0, 59)     ;
  386.     s.dx  = 0;
  387.     s.ax = 0x2d00;
  388.     sysint21(&s, &s);
  389.     if (s.ax & 0xff)
  390.         mPrintf("Invalid time\n ");
  391.     else
  392.         break;
  393.     }
  394. #else
  395.     interpret(pInitDate);
  396. #endif
  397. }
  398.  
  399. /************************************************************************/
  400. /*    transmitFile() dumps a host file with no formatting        */
  401. /************************************************************************/
  402. transmitFile(filename)
  403. char *filename;
  404. {
  405. #define CPMEOF    26            /* For CP/M files, EOF == ^Z    */
  406.     int  getc(), sendWCChar();
  407.     char fname[NAMESIZE];
  408.     long fseek(), totalSectors, fileSize;
  409.     long Secs, Mins, totalTime, totalBytes;
  410.     FILE *fbuf;
  411.     int  c;
  412.  
  413.     outFlag    = OUTOK;
  414.  
  415.     unspace(filename, fname);
  416.  
  417.     if ((fbuf = fopen(fname, "rb")) == NULL) {
  418.     usingWCprotocol = FALSE;
  419.     mPrintf("\n No %s.\n", fname);
  420.     return(ERROR);
  421.     }
  422.  
  423.     doCR();
  424.     if (usingWCprotocol)   {
  425.     usingWCprotocol = FALSE;
  426.     if (byteRate != 0) {
  427.         fileSize = fseek(fbuf, -1l, 2) + 1;
  428.         rewind(fbuf);
  429.         totalSectors = ((fileSize + 127) / fileSize);
  430.         totalBytes     = fileSize + (totalSectors * 5);
  431.         totalTime     = totalBytes/byteRate;     /* In seconds   */
  432.         Mins     = totalTime / 60;
  433.         Secs     = totalTime % 60;
  434.         mPrintf(
  435.       "\n Total time to download '%s' should be about %ld:%02ld minutes",
  436.             fname, Mins, Secs);
  437.     }
  438.  
  439.     if (!getYesNo("Ready for WC transfer")) {
  440.         fclose(fbuf);
  441.         return ERROR;
  442.     }
  443.     usingWCprotocol = TRUE;
  444.     if (!doWC(STARTUP)) {
  445.         fclose(fbuf);
  446.         return ERROR;
  447.     }
  448.     }
  449.  
  450.     while ((c = getc(fbuf)) != ERROR && (c != CPMEOF || !textDownload))  {
  451.     if (!usingWCprotocol) {
  452.         mputChar(c);
  453.         if (interpret(cfg.pCarrDetect)) outMod(c);
  454.     }
  455.     else if (!sendWCChar(c)) break;
  456.     if (!usingWCprotocol && mAbort())  break;
  457.     if (whichIO == MODEM && !gotCarrier()) break;
  458.     }
  459.  
  460.     if (usingWCprotocol)   doWC(FINISH);
  461.  
  462.     fclose(fbuf);
  463. }
  464.  
  465. /************************************************************************/
  466. /*    tutorial() prints file <filename> on the modem & console    */
  467. /*    Returns:    TRUE on success else ERROR            */
  468. /************************************************************************/
  469. #define MAXWORD 256
  470. tutorial(filename)
  471. char *filename;
  472. {
  473.     FILE *fbuf;
  474.     char line[MAXWORD];
  475.     int  toReturn;
  476.  
  477.     toReturn    = TRUE;
  478.  
  479.     outFlag    = OUTOK;
  480.     if (filename[1] == ':' || (fbuf = fopen(filename, "r")) == NULL) {
  481.     mPrintf("\n No %s.\n", filename);
  482.     toReturn    = ERROR;
  483.     } else {
  484.     if (!expert)   mPrintf("\n <J>ump <P>ause <S>top\n");
  485.     mPrintf(" \n");
  486.     while (fgets(line, MAXWORD, fbuf))   mPrintf("%s", line);
  487.     fclose(fbuf);
  488.     }
  489.  
  490.     return   toReturn;
  491. }
  492.  
  493. /************************************************************************/
  494. /*    upLoad() enters a file into current directory            */
  495. /************************************************************************/
  496. upLoad()
  497. {
  498.     char fileName[NAMESIZE], *s;
  499.     int  putFLChar();
  500.     char successful;
  501.  
  502.     getNormStr("filename", fileName, NAMESIZE, ECHO);
  503.  
  504.     if (fileName[1] != ':')   s = &fileName[0];
  505.     else              s = &fileName[2];
  506.  
  507.     if (!setSpace(roomBuf.rbdisk, roomBuf.rbdirname)) {
  508.     crashout("ERROR: DIRECTORY NOT FOUND!");
  509.     }
  510.  
  511.     if ((upfd = fopen(s, "r")) != NULL) {
  512.     mPrintf("\n A %s already exists.\n", s);
  513.     fclose(upfd);
  514.     } else {
  515.     if (!expert) {
  516.         if (strLen(roomBuf.rbdirname) != 0) setSpace(roomBuf.rbdisk, "..");
  517.         setSpace(cfg.homeDisk, "");
  518.         tutorial("wcupload.blb");
  519.         setSpace(roomBuf.rbdisk, roomBuf.rbdirname);
  520.     }
  521.     if ((upfd = fopen(s, "wb")) == NULL) {
  522.         mPrintf("\n Can't create %s!\n", s);
  523.     } else {
  524.         if (!getYesNo("Ready for WC transfer"))
  525.         successful = FALSE;
  526.         else
  527.         successful = readFile(putFLChar);
  528.         fclose(upfd);
  529.         if (!successful) unlink(s);
  530.         else {
  531.         sPrintf(msgBuf.mbtext, "File \"%s\" uploaded into %s by %s.",
  532.               s, roomBuf.rbname, logBuf.lbname);
  533.         aideMessage(FALSE);
  534.         }
  535.     }
  536.     }
  537.     if (strLen(roomBuf.rbdirname) != 0) setSpace(roomBuf.rbdisk, "..");
  538.     setSpace(cfg.homeDisk, "");
  539. }
  540.  
  541. /************************************************************************/
  542. /*    visible() converts given char to printable form if nonprinting    */
  543. /************************************************************************/
  544. char visible(c)
  545. unsigned char c;
  546. {
  547.     if (c==0xFF)  c = '$'    ;   /* start-of-message in message.buf    */
  548.     c            = c & 0x7F    ;   /* kill high bit otherwise        */
  549.     if ( c < ' ') c = c + 'A' -1;   /* make all control chars letters    */
  550.     if (c== 0x7F) c = '~'    ;   /* catch DELETE too         */
  551.     return(c);
  552. }
  553.